const express = require("express");
const router = express.Router();
// const path = require("path");
// const IP2Region = require("ip2region").default;

const db = require("../../config/dbserver.js");
const { setIpJsonDb } = require("../../utils/handIpJson.js");

// 测试接口
router.get("/", async function (req, res) {
  console.log(req.originalUrl);
  const ipxx =
    req.headers["x-forwarded-for"] || req.ip || req.socket.remoteAddress;

    await setIpJsonDb(ipxx);

  //   const query = new IP2Region();
  // const ipreq = query.search(ipxx);

  res.status(200).send({
    code: 200,
    data: {
      // originalUrl: req.originalUrl,
      // ip: req.ip,
      // head: req.headers,
      // ipxx,
      // res : ipreq
    },
    messgae: "test message"
  });
});

router.get("/status", function (req, res) {
  const { code } = req.query;
  const codeList = ["200", "404", "500"];

  if (codeList.includes(code)) {
    res.status(Number(code)).send({
      code: Number(code),
      data: {},
      messgae: "响应数据~~~"
    });
    return;
  } else {
    res.status(200).send({
      code: null,
      data: {},
      messgae: "无对应展示码"
    });
  }
});

router.get("/v", function (req, res) {
  res.status(200).send({
    code: null,
    data: [],
    messgae: "无对应展示码"
  });
});

router.get("/bb", async (req, res) => {
  await db.dbQuery(`INSERT INTO track_log (vv,create_time) VALUES (?,now())`, [
    JSON.stringify({
      info: "未查询到指定oaid",
      value: "asd"
    })
  ]);
});

module.exports = router;
